package widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.dispatchapp.andlei.R;

import java.util.ArrayList;
import java.util.List;

import adapter.DetailAdapter;
import bean.OrderDetailBean;
import bean.TestBean;
import utils.DensityUtil;
import utils.Logger;


/**
 * 详细弹窗
 * Created by Andlei on 2018/7/23.
 */

public class DetailPopupWindow extends PopupWindow {
    private Context mContext;
    private final View mView;
    private RelativeLayout mLlContent;
    private ImageView iv_triangle;
    private RecyclerView recyclerView;
    private DetailAdapter adapter;
    private List<TestBean> list = new ArrayList<>();
    private OrderDetailBean.MoneyListBean moneyListBean;
   // private Activity mActivity;
    public DetailPopupWindow(Context context, OrderDetailBean.MoneyListBean moneyListBean) {
        super(context);
        mContext = context;
        this.moneyListBean = moneyListBean;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.popupwindow_detail,null);
        initView(mView);
    }

    private void initView(View view) {
        mLlContent = view.findViewById(R.id.pop_layout);
        this.setContentView(mView);
        this.setWidth(DensityUtil.dip2px(mContext,175));
        this.setHeight(DensityUtil.dip2px(mContext,230));
        this.setFocusable(true);
        this.setAnimationStyle(R.style.AppTheme);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);

        mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = mView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        hiddenPopupWindow();
                    }
                }
                return true;
            }
        });
        recyclerView = mView.findViewById(R.id.list_rcy);
        iv_triangle = mView.findViewById(R.id.iv_triangle);
        recyclerView.getBackground().setAlpha(200);
        iv_triangle.getBackground().setAlpha(200);
        try {


        TestBean bean = new TestBean();
        bean.str = "基础配送费";
        bean.money = moneyListBean.getBase()+"";
        TestBean bean1 = new TestBean();
        bean1.str = "特殊奖励";
        bean1.money = moneyListBean.getSpecial()+"";
        TestBean bean2 = new TestBean();
        bean2.str = "时段奖励";
        bean2.money = moneyListBean.getTime()+"";
        TestBean bean3 = new TestBean();
        bean3.str = "等级奖励";
        bean3.money = moneyListBean.getLevel()+"";
        TestBean bean4 = new TestBean();
        bean4.str = "距离奖励";
        bean4.money = moneyListBean.getDistance()+"";
        TestBean bean5 = new TestBean();
        bean5.str = "任务奖励";
        bean5.money = moneyListBean.getTask()+"";
        TestBean bean6 = new TestBean();
        bean6.str = "取餐柜费用";
        bean6.money = moneyListBean.getSark_money()+"";
        list.add(bean);
        list.add(bean1);
        list.add(bean2);
        list.add(bean3);
        list.add(bean4);
        list.add(bean5);
        list.add(bean6);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new DetailAdapter(R.layout.item_detail,list);
        recyclerView.setAdapter(adapter);
        }catch (Exception e){
            Logger.e("Exception--->",e);
        }
    }

    public void hiddenPopupWindow(){
        mView.clearAnimation();
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.slide_out);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        mView.startAnimation(animation);
        setAnimation(180,0);

    }

    private void setAnimation(int start, int end) {
        mLlContent.clearAnimation();
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);

        valueAnimator.setDuration(300).start();
        valueAnimator.setTarget(mLlContent);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                getBackground().setAlpha((Integer) animation.getAnimatedValue());
            }
        });
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        mView.clearAnimation();
        mView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in));
        setAnimation(0, 180);
        super.showAtLocation(parent, gravity, x, y);
    }

}















